﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.IsolatedStorage;
using System.IO;
using System.Data.OleDb;
namespace DIProject.ReceptionGoods
{
    public partial class waybillsImport : Form
    {
        public waybillsImport()
        {
            InitializeComponent();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void waybillsImport_Load(object sender, EventArgs e)
        {
            DataSets.SettingsTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.SettingsTableAdapters.ContragentsTableAdapter();
            DataSets.Settings.ContragentsDataTable contrag = adapter.GetData();
            DataSets.Settings.ContragentsRow cr = contrag.NewContragentsRow();
            cr.Id = Int32.MinValue;
            cr.Name = "Выберите поставщика";
            contrag.Rows.InsertAt(cr, 0);
            cbSellers.ComboBox.ValueMember = "Id";
            cbSellers.ComboBox.DisplayMember = "Name";
            cbSellers.ComboBox.DataSource = contrag;
        }

        private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
        {

        }
        private string _currentPath = string.Empty;
        /// <summary>
        /// путь к папке с накладными поставщика.
        /// </summary>
        public string CurrentPath
        {
            get { return _currentPath; }
            set { _currentPath = value; }
        }


        private void cbSellers_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView drv1 = cbSellers.SelectedItem as DataRowView;
            DataSets.Settings.ContragentsRow cr1 = drv1.Row as DataSets.Settings.ContragentsRow;
            if (cr1.Id == Int32.MinValue)
            {
                CurrentPath = string.Empty;
                return;
            }
            string cName = cr1.Name;
            Util.NkldType nType = Util.Setting.GetWaybillsNkldType(cName);
            if (nType == DIProject.Util.NkldType.EMPTY)
            {
                MessageBox.Show("Не задан формат накладной поставщика " + cName, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ToolStripComboBox tscb = sender as ToolStripComboBox;
            DataRowView drv = tscb.SelectedItem as DataRowView;
            DataSets.Settings.ContragentsRow cr = drv.Row as DataSets.Settings.ContragentsRow;
            if (cr.Id == Int32.MinValue)
            {
                CurrentPath = string.Empty;
                return;
            }
            CurrentPath = Util.Setting.GetWaybillsPath(cr.Name);
            if (string.IsNullOrEmpty(CurrentPath))
            {
                MessageBox.Show("Не задан путь к папке в которой храняться накладные поставщика " + cr.Name);
                return;
            }
            string[] aStr = null;
            switch (nType)
            {
                case DIProject.Util.NkldType.APTEKA2002:
                    aStr = Util.File.GetDBFFiles(CurrentPath);
                    break;
                case DIProject.Util.NkldType.MORON:
                    aStr = Util.File.GetDBFFiles(CurrentPath);
                    break;
                case DIProject.Util.NkldType.PROTEK_SST:
                    aStr = DIProject.Util.File.GetSSTFiles1(CurrentPath);
                    break;
                case DIProject.Util.NkldType.CIA:
                    aStr = Util.File.GetDBFFiles(CurrentPath);
                    break;
                default:
                    throw new Util.DIException("Не задан тип файлов поставщика");
            }
             
            List<ReceptionGoods.WaybillInfo> ds = new List<WaybillInfo>();
            switch (nType)
            {
                case DIProject.Util.NkldType.APTEKA2002:
                    DoApteka2002(ds,aStr);
                    break;
                case DIProject.Util.NkldType.MORON:
                    DoMoron1(ds, aStr);
                    break;
                case DIProject.Util.NkldType.PROTEK_SST:
                    DoProtekSST(ds,aStr);
                    break;
                case DIProject.Util.NkldType.CIA:
                    DoCia(ds, aStr);
                    break;
                default:
                    throw new Util.DIException("Не задан тип файлов поставщика");
            }
            DataTable dt = new DataTable();
            dt.Columns.Add("TableName", typeof(string));
            dt.Columns.Add("NumWayBill", typeof(string));
            dt.Columns.Add("DateWaybill", typeof(DateTime));
            foreach (ReceptionGoods.WaybillInfo w in ds)
            {
                DataRow dr = dt.NewRow();
                dr["TableName"] = w.TableName;
                dr["NumWayBill"] = w.NumWayBill;
                dr["DateWaybill"] = w.DateWaybill;
                dt.Rows.Add(dr);
            }
            dgvNaklInfo.DataSource = dt;
        }
       
        private void DoCia(List<ReceptionGoods.WaybillInfo> ds, string[] aStr)
        {
            foreach (string s in aStr)
            {
                ReceptionGoods.WaybillInfo wi = Util.File.GetWaybillInfoCia(CurrentPath, s);
                if (wi != null)
                {
                    ds.Add(wi);
                }
            }
        }
        private void DoMoron1(List<ReceptionGoods.WaybillInfo> ds, string[] aStr)
        {
            foreach (string s in aStr)
            {
                ReceptionGoods.WaybillInfo wi = Util.File.GetWaybillInfo(CurrentPath, s);
                if (wi != null)
                {
                    ds.Add(wi);
                }
            }
        }
        private void DoProtekSST(List<ReceptionGoods.WaybillInfo> ds, string[] aStr)
        {
            foreach (string s in aStr)
            {
                ReceptionGoods.WaybillInfo wi = Util.File.GetWaybillInfoProtek(CurrentPath, s);
                if (wi != null)
                {
                    ds.Add(wi);
                }
            }
        }
        private void DoApteka2002(List<ReceptionGoods.WaybillInfo> ds, string[] aStr)
        {
            foreach (string s in aStr)
            {
                ReceptionGoods.WaybillInfo wi = Util.File.GetWaybillInfoApteka2002(CurrentPath, s);
                if (wi != null)
                {
                    ds.Add(wi);
                }
            }
        }

        private void btnRows_Click(object sender, EventArgs e)
        {
            DataRowView drv = cbSellers.SelectedItem as DataRowView;
            DataSets.Settings.ContragentsRow cr = drv.Row as DataSets.Settings.ContragentsRow;
            string cName = cr.Name;
            Util.NkldType nType = Util.Setting.GetWaybillsNkldType(cName);
            if (nType == DIProject.Util.NkldType.EMPTY)
            {
                MessageBox.Show("Не задан формат накладной поставщика " + cName, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (nType)
            {
                case DIProject.Util.NkldType.MORON:
                    this.DoMoron();
                    break;
                case DIProject.Util.NkldType.PROTEK_SST:
                    DoProtek();
                    break;
                case DIProject.Util.NkldType.APTEKA2002:
                    DoApteka2002();
                    break;
                case DIProject.Util.NkldType.CIA:
                    DoCia();
                    break;
                //case DIProject.Util.NkldType.PROFIT_MED:
                //    DoProfit();
                default:
                    MessageBox.Show("Не определен формат накладной", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }
            
        }
        private DataRow GetWIF
        {
            get
            {
                if (dgvNaklInfo.SelectedRows.Count < 1)
                {
                    MessageBox.Show("Требуется выбрать накладную!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;                    
                }
                return (dgvNaklInfo.SelectedRows[0].DataBoundItem as DataRowView).Row as DataRow;
            }
        }
        /// <summary>
        /// Импорт накладных Профит-мед
        /// </summary>
        private void DoProfit()
        {
            DataRow dr = this.GetWIF;
            if (dr == null)
                return;
            string NumWayBill = dr["NumWayBill"] as string;
            DateTime DateWaybill = (DateTime)dr["DateWaybill"];
            string TableName = dr["TableName"] as string;
            Int32 sellerId = (Int32)cbSellers.ComboBox.SelectedValue;
            DataSets.waybillsData.MoronDataTable moronTable = Util.File.GetCIAData(CurrentPath, TableName);
            ReceptionGoods.MoronRows mf = new MoronRows(moronTable, sellerId, NumWayBill, DateWaybill);
            mf.MdiParent = this.MdiParent;
            mf.Show();        
        }

        private void DoCia()
        {
            DataRow dr = this.GetWIF;
            if (dr == null)
                return;
            string NumWayBill = dr["NumWayBill"] as string;
            DateTime DateWaybill = (DateTime)dr["DateWaybill"];
            string TableName = dr["TableName"] as string;
            Int32 sellerId = (Int32)cbSellers.ComboBox.SelectedValue;
            DataSets.waybillsData.MoronDataTable moronTable = Util.File.GetCIAData(CurrentPath, TableName);
            ReceptionGoods.MoronRows mf = new MoronRows(moronTable, sellerId, NumWayBill, DateWaybill);
            mf.MdiParent = this.MdiParent;
            mf.Show();
        }
        private void DoMoron()
        {
            DataRow dr = this.GetWIF;
            if (dr == null)
                return;
            string NumWayBill = dr["NumWayBill"] as string;
            DateTime DateWaybill = (DateTime)dr["DateWaybill"];
            string TableName = dr["TableName"] as string;
            Int32 sellerId = (Int32)cbSellers.ComboBox.SelectedValue;
            DataSets.waybillsData.MoronDataTable moronTable = Util.File.GetMoronData(CurrentPath, TableName);
            ReceptionGoods.MoronRows mf = new MoronRows(moronTable, sellerId, NumWayBill, DateWaybill);
            mf.MdiParent = this.MdiParent;
            mf.Show();
        }
        private void DoApteka2002()
        {
            DataRow dr = this.GetWIF;
            if (dr == null)
                return;
            string NumWayBill = dr["NumWayBill"] as string;
            DateTime DateWaybill = (DateTime)dr["DateWaybill"];
            string TableName = dr["TableName"] as string;
            Int32 sellerId = (Int32)cbSellers.ComboBox.SelectedValue;
            DataSets.waybillsData.MoronDataTable moronTable = Util.File.GetApteka2002Data(CurrentPath, TableName);
            ReceptionGoods.MoronRows mf = new MoronRows(moronTable, sellerId, NumWayBill, DateWaybill);
            mf.MdiParent = this.MdiParent;
            mf.Show();
        }
        private void DoProtek()
        {
            try
            {
                DataRow dr = this.GetWIF;
                if (dr == null)
                    return;
                string NumWayBill = dr["NumWayBill"] as string;
                DateTime DateWaybill = (DateTime)dr["DateWaybill"];
                string TableName = dr["TableName"] as string;
                Int32 sellerId = (Int32)cbSellers.ComboBox.SelectedValue;
                DataSets.waybillsData.MoronDataTable moronTable = Util.File.GetProtekData(CurrentPath, TableName);
                ReceptionGoods.MoronRows mf = new MoronRows(moronTable, sellerId, NumWayBill, DateWaybill);
                mf.MdiParent = this.MdiParent;
                mf.Show();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
              
    }

}